
const app = getApp();
Page({
  data: {
    id:"",
    newid:"",
    title: '相关条款',
    name: '',
    phoneNumber: '',
    identityCard: '',
    bankName: '',
    bankId: '',
    isdisabled: false,
    loading: false,
    banklist: [],
    cardNumber:"",
    normalBankCard:""
  },
  onShow: function () {
    const _this = this;
    wx.getStorage({
      key: 'banks',
      success: function (res) {
        _this.setData({
          bankName: res.data.bankName,
          bankId: res.data.bankId,
        })
        console.log(111111)
        console.log(res.data);
      }
    })
    wx.removeStorage({
      key: 'banks',
      success: function (res) {
        console.log(2222)
        console.log(res.data)
      }
    })
  },
  onLoad: function (options) {
    const _this = this;
    
    if (options.id){
        this.setData({
          newid:options.id
        })
      wx.setNavigationBarTitle({
        title: '修改成员'
      })
      this.getInfo()
    }
    // console.log(options.bankName);
   
    //修改页面标题
  
    this.getAgreeMent();
  //  this.getInfo();
    // wx.getStorage({
    //   key: 'changecard',
    //   success: function (res) {
    //     _this.setData({
    //       cardNumber: res.data.cardNumber
    //     })
    //     console.log(res.data);
    //   }
    // })
  },
  //是否同意
  // checkboxChange: function (e) {
  //   if (e.detail.value.length == 0) {
  //     this.setData({
  //       check: 0  //未选
  //     })
  //   }
  //   else {
  //     this.setData({
  //       check: 1  //已选
  //     })
  //   }
  //   // console.log(this.data.check)
  // },
  //用户信息
  getInfo() {
    const _this = this;
    wx.request({
      url: app.globalData.path + 'services/app/applets/TeamDetail',
      data: {
        id: _this.data.newid
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          console.log("user")
          console.log(res.data)
          _this.setData({
            name: res.data.result.name,
            phoneNumber: res.data.result.phoneNumber,
            identityCard: res.data.result.identityCard,
            bankName: res.data.result.bankName,
            //id: res.data.result.bankCode,
            cardNumber: res.data.result.normalBankCard,
            normalBankCard: res.data.result.normalBankCard
          })
          _this.getBankList(res.data.result.bankCode);
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 条款
  getAgreeMent: function () {
    this.setData({
      title: app.globalData.agreementTitle
    })
  },
  //名字
  getusername:function(e){
    this.setData({
      name: e.detail.value
    })
    console.log(this.data.name)
  },
  getuserphoneNumber:function(e){
    this.setData({
      phoneNumber: e.detail.value
    })
  },
  getuseridentityCard:function(e){
    this.setData({
      identityCard: e.detail.value
    })
  },
  // 自动匹配银行卡
  getBankNumber: function (e) {
    // console.log(e.detail.value);
    const _this = this;
    _this.setData({
      cardNumber: e.detail.value,
      normalBankCard: e.detail.value
    })
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetBankShortName',
      data: {
        _input_charset: 'utf-8',
        cardNo: e.detail.value
      },
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        console.log(res.data)
        if (res.data.success) {
          if (res.data.result.validated) {
            _this.setData({
              id: res.data.result.bank,
              isdisabled: false
            })
            _this.getBankList(_this.data.id);
          } else {
            _this.setData({
              id: ''
            })
          }
          if (!res.data.result.bank) {
            console.log("不存在")
            wx.showToast({
              title: '暂不支持该行，请更换银行卡！',
              icon: 'none',
              duration: 2000
            })
            _this.setData({
              isdisabled: true
            })
          }
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      },
      complete: function (res) {
        // wx.showToast({
        //   title: '1111111111111111111111111',
        //   icon: 'success',
        //   duration: 2000
        // })
      }
    })
  },

  // 银行卡信息获得
  getBankList: function (id) {
    const _this = this;
    wx.request({
      url: app.globalData.path + 'services/app/applets/GetBankList',
      data: {
        bankCode: id
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(res.data);
        if (res.data.success) {
          if (res.data.result.items.length > 1) {
            _this.setData({
              bankName: '',
              bankId: '',
              banklist: res.data.result.items
            })
          } else if (res.data.result.items.length == 1) {
            _this.setData({
              bankName: res.data.result.items[0].bankName,
              bankId: res.data.result.items[0].id
            })
          } else if (res.data.result.items.length == 0) {
            wx.showToast({
              title: '暂不支持该行，请更换银行卡！',
              icon: 'none',
              duration: 2000
            })
            _this.setData({
              isdisabled: true
            })
          }
        } else {
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // 验证身份证
  isCardNoTrue: function (card) {
    // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X 
    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (reg.test(card) === false) {
      wx.showModal({
        content: '身份证格式不正确',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          }
        }
      });
      return false;
    }
    return true;
  },
  //添加成员
adduser:function(e){
  let that = this
  console.log(e.detail)
  if (that.isCardNoTrue(e.detail.value.idNumber)){
    wx.request({
      url: app.globalData.path + 'services/app/applets/Create_TeamTransfer',
      data: {
        name: e.detail.value.username,
        phoneNumber: e.detail.value.phone,
        identityCard: e.detail.value.idNumber,
        bankCard: e.detail.value.cardNumber,
        bankId: e.detail.value.bankId
      },
      method: 'POST',
      header: {
        'content-type': 'application/json', // 默认值
        'Authorization': 'Bearer ' + app.globalData.authorization
      },
      success: function (res) {
        console.log(11111111)
        console.log(res.data);
        if (res.data.success) {
          wx.navigateBack({
            delta: 1
          })
          that.setData({
            loading: false,
            isdisabled: false
          })
        } else {
          that.setData({
            loading: false,
            isdisabled: false
          })
          wx.showToast({
            title: res.data.error.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  }else{
    that.setData({
      loading: false,
      isdisabled: false
    })
  }
 
 
 
},
  //修改成员
edituser(e){
let that=this
  console.log("修改成员")
  console.log(e.detail.value)
  wx.request({
    url: app.globalData.path + 'services/app/applets/Update_TeamTransfer',
    data: {
      userId: that.data.newid,
      bankCard: that.data.normalBankCard,
      bankId: e.detail.value.bankId
    },
    method: 'POST',
    header: {
      'content-type': 'application/json', // 默认值
      'Authorization': 'Bearer ' + app.globalData.authorization
    },
    success: function (res) {
      console.log(11111111)
      console.log(res.data);
      if (res.data.success) {
        wx.navigateBack({
          delta: 1
        })
        that.setData({
          loading: false,
          isdisabled: false
        })
      } else {
        that.setData({
              loading: false,
              isdisabled: false
            })
        wx.showToast({
          title: res.data.error.message,
          icon: 'none',
          duration: 2000
        })
      }
    }
  })
},
  // 下一步
  formSubmit: function (e) {
    const _this = this;
    e.detail.value.bankId = _this.data.bankId
    e.detail.value.normalBankCard = _this.data.normalBankCard
    if (e.detail.value.cardNumber == '' || _this.data.bankId == '') {
      wx.showModal({
        content: '请将信息填写完整',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            // console.log('用户点击确定')
          }
        }
      });
    } else {
      if (e.detail.value.phone.length!=11){
        wx.showModal({
          content: '手机号格式不正确',
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        });
       
      }else{
        wx.removeStorage({
          key: 'changecard',
          success: function (res) {
            console.log(res.data)
          }
        })
        _this.setData({
          loading: true,
          isdisabled: true
        })
        if (_this.data.newid) {
          _this.edituser(e)
        } else {
          _this.adduser(e)
        }
      }
    


      // wx.request({
      //   url: app.globalData.path + 'services/app/applets/ChangeBankCard',
      //   data: {
      //     bankCode: _this.data.bankId,
      //     bankCard: e.detail.value.cardNumber
      //   },
      //   method: 'POST',
      //   header: {
      //     'content-type': 'application/json', // 默认值
      //     'Authorization': 'Bearer ' + app.globalData.authorization
      //   },
      //   success: function (res) {
      //     console.log(res.data)
      //     if (res.data.success) {
      //       // _this.setData({
      //       //   loading: false,
      //       //   isdisabled: false
      //       // })
      //       // wx.navigateTo({
      //       //   url: '../my/bank/bank'
      //       // })
      //       if (_this.data.newid){
      //         _this.adduser(e)
      //       }else{
      //         _this.edituser(e)
      //       }
      //     } else {
      //       _this.setData({
      //         loading: false,
      //         isdisabled: false
      //       })
      //       if (res.data.error.code == -1) {
      //         wx.showToast({
      //           title: '请添加与员工信息一致的银行卡',
      //           icon: 'none',
      //           duration: 2000
      //         })
      //       } else {
      //         wx.showModal({
      //           content: res.data.error.message,
      //           showCancel: false,
      //           success: function (res) {
      //             if (res.confirm) {
      //               console.log('用户点击确定')
      //             }
      //           }
      //         });
      //       }
      //     }
      //   }
      // })
    }
  },
  //删除
  deleteuser:function(){
    console.log(111)
   let that=this
    wx.showModal({
      title:"是否删除",
      success:function(res){
        console.log(res.confirm)
        if (res.confirm){
          wx.request({
            url: app.globalData.path + 'services/app/applets/Delete_TeamTransfer',
            data: {
              id: that.data.newid
            },
            method: 'POST',
            header: {
              'content-type': 'application/json', // 默认值
              'Authorization': 'Bearer ' + app.globalData.authorization
            },
            success: function (res) {
              console.log(11111111)
              console.log(res.data);
              if (res.data.success) {
                wx.navigateBack({
                  delta: 1
                })
              } else {
                wx.showToast({
                  title: res.data.error.message,
                  icon: 'none',
                  duration: 2000
                })
              }
            }
          })
        }
      }
    })


    // wx.navigateTo({
    //   url: '../teamuser/teamuser'
    // })
  },
  toBanks: function () {
    const _this = this;
    // wx.setStorage({
    //   key: 'changecard',
    //   data: {
    //     cardNumber: _this.data.cardNumber
    //   }
    // })
    wx.navigateTo({
      url: '../banks/banks?from=change',
    })
  },
  // 协议
  // agreeMent: function () {
  //   wx.navigateTo({
  //     url: '../agreement/agreement',
  //   })
  // }
})